package com.ryx.service.Impl;

import com.github.pagehelper.PageHelper;
import com.ryx.entity.SysRotationChart;
import com.ryx.exception.GlobalException;
import com.ryx.exception.code.BaseResponseCode;
import com.ryx.mapper.SysRotationChartMapper;
import com.ryx.service.FileService;
import com.ryx.service.RotationChartService;
import com.ryx.utils.PageUtil;
import com.ryx.vo.req.RotationChartDeleteReqVo;
import com.ryx.vo.req.RotationChartReqAddVo;
import com.ryx.vo.req.RotationChartUpdateReqVo;
import com.ryx.vo.req.RotationPageReqVo;
    
import com.ryx.vo.resp.PageVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @BelongsPackage: com.ryx.service.Impl
 * @Author: 容永轩
 * @CreateTime: 2021-01-28
 * @Description:
 */
@Service
@Slf4j
public class RotationChartServiceImpl implements RotationChartService {

    @Autowired
    private SysRotationChartMapper sysRotationChartMapper;

    @Autowired
    private FileService fileService;

    @Override
    public PageVo<SysRotationChart> pageInfo(RotationPageReqVo vo) {
        PageHelper.startPage(vo.getPageNum(),vo.getPageSize());

        List<SysRotationChart> sysRotationCharts = sysRotationChartMapper.selectAll();

        return PageUtil.getPageVo(sysRotationCharts);
    }

    @Override
    public List<SysRotationChart> selectAll() {
        return sysRotationChartMapper.selectAll();
    }

    @Override
    public void addRotation(RotationChartReqAddVo vo, String userId) {

        SysRotationChart sysRotationChart = new SysRotationChart();
        BeanUtils.copyProperties(vo,sysRotationChart);

        sysRotationChart.setCreateId(userId);
        sysRotationChart.setCreateTime(new Date());
        sysRotationChart.setId(UUID.randomUUID().toString());


        int i = sysRotationChartMapper.insertSelective(sysRotationChart);

        if (i !=1){
            throw new GlobalException(BaseResponseCode.OPERATION_ERROR);
        }


    }

    @Override
    public void updateRotationInfo(RotationChartUpdateReqVo vo, String userId) {

        //首先查到之前的轮播图信息
        SysRotationChart sysRotationChart = sysRotationChartMapper.selectByPrimaryKey(vo.getId());

        if (sysRotationChart == null){
            log.error("传入 的 id:{}不合法",vo.getId());
            throw new GlobalException(BaseResponseCode.DATA_ERROR);
        }

        //判断图片有唔变化
        if (!sysRotationChart.getFileUrl().equals(vo.getFileUrl())){
            //发生变化的话 就把原来的data删掉
            fileService.deleteFile(sysRotationChart.getFileUrl());
        }

        BeanUtils.copyProperties(vo,sysRotationChart);
        sysRotationChart.setUpdateTime(new Date());
        sysRotationChart.setUpdateId(userId);
        int i = sysRotationChartMapper.updateByPrimaryKeySelective(sysRotationChart);
        if (i !=1){
            throw new GlobalException(BaseResponseCode.OPERATION_ERROR);
        }


    }

    @Override
    public int batchDeleteRotation(List<RotationChartDeleteReqVo> list) {

        int i = sysRotationChartMapper.batchDeleteRotation(list);

        if (i <= 0){
            throw new GlobalException(BaseResponseCode.OPERATION_ERROR);
        }

        //删除磁盘data
        for (RotationChartDeleteReqVo rotationChartDeleteReqVo : list) {
            fileService.deleteFile(rotationChartDeleteReqVo.getFileUrl());
        }

        return i;
    }
}
